// pages/first/zixun/zixun.js
import { request } from '../../../utils/http.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    zixunlist:[],
    tid:0,
    "show1": false,
    "show2": false,
    "show3": false,
    "level": '选择等级',
    "selected_level": 1,
    "qk_text": "",
    "qk_click": false,
    "temp_text": "",
    "levels": [{"id":1,"level":"国家一级心理咨询师"},
        {"id":2,"level":"国家二级心理咨询师"},
        {"id":3,"level":"国家三级心理咨询师"}],
    "date": '请选择日期',
    datetime: "9:30",
    time:'',
    show_time: false,
    filter(type, options) {
      if (type === 'minute') {
        return options.filter((option) => option % 15 === 0);
      }
      if (type === 'hour') {
        return options.filter((option) => option != 12 && option != 13 && option != 14);
      }
      return options;
    },
    "selected_type": 1,
    "selected_type_text": "家庭关系",
    "consult_types": [
      {"id":1,"type":"家庭关系"},{"id":2,"type":"亲友关系"},
      {"id":3,"type":"婚恋关系"},{"id":4,"type":"生活问题"},
      {"id":5,"type":"个体咨询"},{"id":6,"type":"人际关系"},
      {"id":7,"type":"压力情绪"},{"id":8,"type":"儿童心理"},
      {"id":9,"type":"青少年"},{"id":10,"type":"成年人"},
      {"id":11,"type":"职场问题"}
    ]
  },
  onTimeCheck(msg) {
    this.setData({
      datetime: msg.detail,
      show3: false
    });
  },
  click_date(msg) {
    this.setData({
      show_time: false
    });
  },
  click_time(msg) {
    this.setData({
      show_time: true
    });
  },
  click_type(msg) {
    // console.log(msg);
    this.setData({
      "selected_type": msg.currentTarget.id,
      "selected_type_text": msg.currentTarget.dataset.text
    });
    this.onClose2();
  },
  click_level(msg) {
    this.setData({
      "selected_level": msg.currentTarget.id,
      "level": msg.currentTarget.dataset.text
    });
    this.onClose1();
  },
  formatDate(date) {
    date = new Date(date);
    console.log(date);
    return `${(date.getYear()-100)+2000}/${date.getMonth() + 1}/${date.getDate()}`;
  },
  onSelected(msg) {
    console.log("选中日期");
    console.log(msg);
    this.setData({
      show3: false,
      date: this.formatDate(msg.detail),
    });
  },

  //
  select_level(msg) {
    console.log(msg);
    this.setData({
      "show1": true
    });
  },
  select_type(msg) {
    console.log(msg);
    this.setData({
      "show2": true
    });
  },
  select_time(msg) {
    console.log(msg);
    this.setData({
      "show3": true
    });
  },
  select_qk(msg) {
    this.setData({
      qk_click: true,
      temp_text: this.data.qk_text
    });

  },
  input_qk(msg) {
    this.setData({
      qk_text: msg.detail.value
    });
  },
  onClose1(msg) {
    console.log("关闭");
    this.setData({
      "show1": false
    });
  },
  onClose2(msg) {
    console.log("关闭");
    this.setData({
      "show2": false
    });
  },
  onClose3(msg) {
    console.log("关闭");
    this.setData({
      "show3": false
    });
  },
  check_button(msg) {
    this.setData({
      time:this.data.date+" "+this.data.datetime
    })
    if(this.data.qk_click == true) {
      this.setData({
        qk_click: false,
      });
    } else {
      if(this.data.tid != 0){
        request({
          url: '/zixunyuyue/addzixun',
          method: 'GET',
          data: { tid:this.data.tid,
                  uid:getApp().globalData.uid,
                  level:this.data.level,
                  selected:this.data.selected_type_text,
                  time:this.data.time,
                  text:this.data.qk_text
          }
        }).then((data) => {
          console.log(data);
          console.log("-------------------");
        }).catch((err) => {
          console.error(err);
        });
        wx.navigateTo({
          url: '/pages/talk/xiangqing/xiangqing?tid='+this.data.tid,
        });
      }else{
        if(getApp().globalData.uid != null) {
          wx.navigateTo({
            url: '/pages/first/zixun_select/zixun_select?uid='+getApp().globalData.uid+'&level='+this.data.level+'&selected='+this.data.selected_type_text+'&time='+this.data.time+'&text='+this.data.qk_text,
          });
        } else {
            wx.navigateTo({
                url: '/pages/my/my_pages/my_login/my_login',
            })
        }
      }
    }
  },
  cancil_button(msg) {
    if(this.data.qk_click == true) {
      this.setData({
        qk_click: false,
        qk_text: this.data.temp_text
      });
    } else {
      wx.switchTab({
              url: '/pages/first/first',
            });
    }
      
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    this.setData({
      level:options.levelName
    })
    if ('id' in options){
      this.setData({
        tid:options.id
      })
    }

    request({
      url: '/zixunleibie/listAll',
      method: 'GET',
      data: { tid:options.tid }
    }).then((data) => {
      console.log(data);
      console.log(data.data);
      this.setData({
        zixunlist: data.data,
      });
    }).catch((err) => {
      console.error(err);
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})